/******************************************************************************************************
Copyright (C) 2012 Mestrelab Research S.L. All rights reserved.

This file is part of the MNova scripting toolkit.

Authorized users of MNova Software may use this file freely, but this file is provided AS IS
with NO WARRANTY OF ANY KIND, INCLUDING THE WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS
FOR A PARTICULAR PURPOSE.
*****************************************************************************************************/

/** The Env.DefaultNMRProcessingSetup can be set to a function that will be called everytime a NMR spectrum is imported. If an exception is thrown or the value returned is an error or a boolean false then Mnova will use the default processing. No returned value is interpreted as no error.
 * The function will receive as "this" object the NMRProcessing object initialized by Mnova that will be applied to the spectrum and as a parameter an object containing the relevant information parsed at this moment from the file. 
 * Properties available in the spectrum info object:
 * - coord[]: an array in which the nth element will have this info about the nth dimension of the experiment: pts,zero,width,Hz,MHz,nucleus,Ph0,Ph1
 * - originalFormat: a string with the imported format
 * - other parameters avaliable: DataFileName, Title, Comment, Origin, Owner, Site, Spectrometer, Author, Solvent, Temperature, PulseSequence, Experiment, NumberofScans, ReceiverGain, RelaxationDelay, PulseWidth, AcquisitionTime, AcquisitionDate, ModificationDate, SpectrometerFrequency, SpectralWidth, LowestFrequency, Nucleus, AcquiredSize, SpectralSize.
 * For 1D, all of them are strings, for 2D experiments some of this parameters are arrays having the information about the nth dimension in the nth element. Use print(JSON.stringify(aSpcInfo)) to see all the properties
 * Check example functions below.
 */
Env.DefaultNMRProcessingSetup = noProcessing
//Env.DefaultNMRProcessingSetup = clearMnovaProcessing
//Env.DefaultNMRProcessingSetup = baselineCorrection
//Env.DefaultNMRProcessingSetup = processingFormat
//Env.DefaultNMRProcessingSetup = processingFromFile
//Env.DefaultNMRProcessingSetup = apodization

/**
 * This function will not process the spectrum. The original FID will be shown
 */
function noProcessing(aSpcInfo)
{
	print(JSON.stringify(aSpcInfo))
	if(aSpcInfo.nDim==1) {
		this.stopProc = NMRProcessing.OriginalFID
	}
}

/**
 * Mnova will always construct a default processing object based on the properties of the spectrum. If the user wants to have full control of the initial processing, before modifying the "this" object setApplyNothing must be called
*/
function clearMnovaProcessing(aSpcInfo)
{
	//print(JSON.stringify(aSpcInfo))
	this.setApplyNothing()
	
	//add processing here...
}


/**
 * This function will apply a baseline correction using Bernstein algorithm only for 1D experiments
 */
function baselineCorrection(aSpcInfo)
{
	//print(JSON.stringify(aSpcInfo))
	if(aSpcInfo.nDim==1) {
		this.setParameter("BC[1].apply",true)
		this.setParameter("BC[1].algorithm","Bernstein")
	}
}

/**
 * This function will load a processing template from a file
 */
function processingFromFile(aSpcInfo)
{
	//print(JSON.stringify(aSpcInfo))
	if(this.open("/home/isaac/tmp/myProcTemplate.mnp")==false) //import proc
		MessageBox.critical("Failed to open processing template")//this will show the message but Mnova will open the file and use the default processing
}

/**
 * This function will apply apodization to the imported spectrum
 */
function apodization(aSpcInfo)
{
	//print(JSON.stringify(aSpcInfo))
	this.setParameter("Apodization[1].Exp.apply", true)
	this.setParameter("Apodization[1].Exp.value", 0.5)
	this.setParameter("Apodization[1].fp.apply", true)
	this.setParameter("Apodization[1].fp.value", 0.5)
}

/**
 * This function will apply different processing templates based on the original format. Check NMRSpectrum.originalFormat for information about the possible values.
 */
function processingFormat(aSpcInfo)
{
	//print(JSON.stringify(aSpcInfo))
	if(aSpcInfo.originalFormat=="Varian") {
		this.stopProc = NMRProcessing.ProcessedFID //no proc
	} else if(aSpcInfo.originalFormat=="JEOL Delta") {
		if(this.open("/home/isaac/tmp/myProcTemplate.mnp")==false) //import proc
			MessageBox.critical("Failed to open processing template!")
	} else if( aSpcInfo.originalFormat=="Bruker WIN-NMR") { 
		if(aSpcInfo.nDim==1) {//modify apodization
			this.setParameter("Apodization[1].Exp.apply", true)
			this.setParameter("Apodization[1].Exp.value", 0.5)
			this.setParameter("Apodization[1].fp.apply", true)
			this.setParameter("Apodization[1].fp.value", 0.5)
		}
	}
}
